/**
 * 网络通信类型
 * 2016.10.01
 */
module SocketManager {
    export var socket_list = {};
    //创建连接(此方法不允许外外部调用,发消息请用 sendMessage)
    function connectServer(gameType: string, byet: any = null, isShowWait: boolean = false) {
        let socket: SocketManager.socket = socket_list[gameType];
        if (socket) {
            socket.closeSocket();
        }
        let ip = null;
        let port = null;
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.HALL) {
            ip = InstructionsEnum.IP_LOGIN;
            port = InstructionsEnum.PORT_HALL;
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.CHAT) {
            ip = InstructionsEnum.IP_LOGIN;
            port = InstructionsEnum.PORT_CHAT;
        } else if (gameType) {
            ip = InstructionsEnum.IP_LOGIN;
            port = InstructionsEnum.PORT_GAME;
        }
        //创建链接时区分某些服务器需要启用心跳 目前只有聊天服需要启动心跳
        var heartCheckEnable = false;
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.CHAT) {
            heartCheckEnable = true;
        }
        socket = new SocketManager.socket(gameType, ip, port, byet, isShowWait, heartCheckEnable);
        socket_list[gameType] = socket;
    }
    //某socket对象被异常关闭
    export function onSocketClose(socket: SocketManager.socket) {
        if (socket.gameType == InstructionsEnum.GAME_TYPE_NAME.HALL) {
            //主页连接断开，跳出到登陆页
            SocketManager.closeAllSocket();
            GameLayerManager.gameLayer().panelLayer.removeChildren();
        } else {
            //游戏连接端口跳转到主页
            closeSocket(socket.gameType);
        }
        Global.removeInterval();
        Global.removeTimeOut();
    }
    //关闭指定连接
    export function closeSocket(gameType: string) {
        let socket: SocketManager.socket = socket_list[gameType];
        if (!socket) {
            return;
        }
        socket.closeSocket()
        delete socket_list[gameType];
        socket = null;
        //游戏连接掉线关闭游戏界面
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.DDZ) {
            game.AppFacade.instance.sendNotification(PanelNotify.DDZ.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.BRNN) {
            game.AppFacade.instance.sendNotification(PanelNotify.BRNN.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.HLSZ) {
            game.AppFacade.instance.sendNotification(PanelNotify.HLSZ.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.QZNN) {
            game.AppFacade.instance.sendNotification(PanelNotify.QZNN.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.WJL) {
            game.AppFacade.instance.sendNotification(PanelNotify.WJL.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.LHD) {
            game.AppFacade.instance.sendNotification(PanelNotify.LHD.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.HHDZ) {
            game.AppFacade.instance.sendNotification(PanelNotify.HHDZ.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.HCJLB) {
            game.AppFacade.instance.sendNotification(PanelNotify.HCJLB.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.BREBG) {
            game.AppFacade.instance.sendNotification(PanelNotify.BREBG.CLOSE_PLAY_GAME);
        } else if (gameType == InstructionsEnum.GAME_TYPE_NAME.HALL) {
            GameLayerManager.gameLayer().panelLayer.removeChildren();
        }
        //游戏掉线要刷新大厅金币
        if (gameType != InstructionsEnum.GAME_TYPE_NAME.CHAT &&
            gameType != InstructionsEnum.GAME_TYPE_NAME.HALL) {
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.BACK_HALL_BY_GAME)
        }
    }
    //关闭所有连接
    export function closeAllSocket() {
        for (let key in socket_list) {
            closeSocket(key);
        }
    }
    /**
     * gameType:指定哪个游戏的socket对象发送
     * mainId：指定打包方式
     * sendMes：要发送的JSON数据
     * isShowWait:是否显示转圈等待
     */
    export function sendMessage(gameType: string, mainId: any = null, sendMes: any = null, isShowWait: boolean = false): void {
        if (InstructionsEnum.IS_TEST) {
            console.log(gameType + "发送:", mainId, sendMes)
        }
        let byet: egret.ByteArray = DataBuilder.WSSendMsg(mainId, sendMes);
        let socket: SocketManager.socket = socket_list[gameType];
        if (socket == null) {
            connectServer(gameType, byet, isShowWait);
        } else {
            socket.sendMessage(byet, isShowWait);
        }
    }
    //消息返回
    export function onReceiveMessage(gameType: string, data: any) {
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.HALL) {
            game.AppFacade.getInstance().sendNotification(game.Processor_GAME.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.CHAT) {
            game.AppFacade.getInstance().sendNotification(game.Processor_CHAT.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.BRNN) {
            game.AppFacade.getInstance().sendNotification(game.Processor_BRNN.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.DDZ) {
            game.AppFacade.getInstance().sendNotification(game.Processor_DDZ.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.QZNN) {
            game.AppFacade.getInstance().sendNotification(game.Processor_QZNN.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.HLSZ) {
            game.AppFacade.getInstance().sendNotification(game.Processor_HLSZ.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.WJL) {
            game.AppFacade.getInstance().sendNotification(game.Processor_WJL.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.LHD) {
            game.AppFacade.getInstance().sendNotification(game.Processor_LHD.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.HHDZ) {
            game.AppFacade.getInstance().sendNotification(game.Processor_HHDZ.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.HCJLB) {
            game.AppFacade.getInstance().sendNotification(game.Processor_HCJLB.NAME, data);
        }
        if (gameType == InstructionsEnum.GAME_TYPE_NAME.BREBG) {
            game.AppFacade.getInstance().sendNotification(game.Processor_BREBG.NAME, data);
        }
    }
    //进入后台时关闭游戏连接
    export function gameOnPause() {
        //游戏切换到前台时，找到当前正在玩的游戏，断开连接并通知大厅重新登陆这个游戏
        for (let type in socket_list) {
            let socket: SocketManager.socket = socket_list[type];
            if (socket.gameType && socket.gameType != InstructionsEnum.GAME_TYPE_NAME.HALL) {
                closeSocket(socket.gameType);
                break;
            }
        }
    }
    //进入前台时重连到游戏服务器
    export function gameOnResume() {
        if (Global.isNotEmpty(InstructionsEnum.ID_GAME)) {
            game.AppFacade.instance.sendNotification(PanelNotify.HALL.HALL_RECONNECTGAME, InstructionsEnum.ID_GAME)
        }
    }
}



